package template

import (
	"io/ioutil"
	"os"
	"strings"
)

var (
	projectReplacer      *strings.Replacer
	projectProtoReplacer *strings.Replacer
	versionReplacer      *strings.Replacer
)

type commonKind struct{}

func (c *commonKind) Kind() string {
	return "common"
}

func (c *commonKind) Gen(rootDir string) error {
	// model
	{
		argsDir := rootDir + "/model/args"
		err := os.MkdirAll(argsDir, 0775)
		if err != nil {
			return err
		}
		argsConst := projectReplacer.Replace(templateModelArgsConst)
		err = ioutil.WriteFile(argsDir+"/const.go", []byte(argsConst), 0666)
		if err != nil {
			return err
		}
		mysqlDir := rootDir + "/model/mysql"
		err = os.MkdirAll(mysqlDir, 0775)
		if err != nil {
			return err
		}
		mysqlTable := projectReplacer.Replace(templateModelMysqlTable)
		err = ioutil.WriteFile(mysqlDir+"/table.go", []byte(mysqlTable), 0666)
		if err != nil {
			return err
		}
	}
	// pkg util
	{
		codeDir := rootDir + "/code"
		err := os.MkdirAll(codeDir, 0775)
		if err != nil {
			return err
		}
		serviceCode := codeDir + "/code.go"
		err = ioutil.WriteFile(serviceCode, []byte(templatePkgCode), 0666)
		if err != nil {
			return err
		}
		utilDir := rootDir + "/util"
		err = os.MkdirAll(utilDir, 0775)
		if err != nil {
			return err
		}
		emailDir := utilDir + "/email"
		err = os.MkdirAll(emailDir, 0775)
		if err != nil {
			return err
		}
		err = ioutil.WriteFile(emailDir+"/email.go", []byte(templatePkgUtilEmail), 0666)
		if err != nil {
			return err
		}
		emailHelperData := projectReplacer.Replace(templatePkgUtilEmailHelper)
		err = ioutil.WriteFile(emailDir+"/email_helper.go", []byte(emailHelperData), 0666)
		if err != nil {
			return err
		}
		err = ioutil.WriteFile(utilDir+"/client.go", []byte(templatePkgUtilClient), 0666)
		if err != nil {
			return err
		}
	}
	// startup
	{
		startupDir := rootDir + "/startup"
		err := os.MkdirAll(startupDir, 0775)
		if err != nil {
			return err
		}
		err = ioutil.WriteFile(startupDir+"/vars.go", []byte(templateStartupVars), 0666)
		if err != nil {
			return err
		}
	}

	// script
	{
		buildData := projectReplacer.Replace(templateBuildShell)
		err := ioutil.WriteFile(rootDir+"/build.sh", []byte(buildData), 0666)
		if err != nil {
			return err
		}
		buildRunData := projectReplacer.Replace(templateBuildRunShell)
		err = ioutil.WriteFile(rootDir+"/build-run.sh", []byte(buildRunData), 0666)
		if err != nil {
			return err
		}
		cleanBinData := projectReplacer.Replace(templateCleanBinShell)
		err = ioutil.WriteFile(rootDir+"/clean-bin.sh", []byte(cleanBinData), 0666)
		if err != nil {
			return err
		}
		err = ioutil.WriteFile(rootDir+"/clean-logs.sh", []byte(templateCleanLogsShell), 0666)
		if err != nil {
			return err
		}
		dockerData := projectReplacer.Replace(templateDockerFile)
		err = ioutil.WriteFile(rootDir+"/Dockerfile", []byte(dockerData), 0666)
		if err != nil {
			return err
		}
		err = ioutil.WriteFile(rootDir+"/genpb.py", []byte(templateGenPbPy), 0666)
		if err != nil {
			return err
		}
		xormReData := projectReplacer.Replace(templateXormReveRseYaml)
		err = ioutil.WriteFile(rootDir+"/xorm-db.yaml", []byte(xormReData), 0666)
		if err != nil {
			return err
		}
		restartShellData := projectReplacer.Replace(templateRestartShell)
		err = ioutil.WriteFile(rootDir+"/restart.sh", []byte(restartShellData), 0666)
		if err != nil {
			return err
		}
		startShellData := projectReplacer.Replace(templateStartShell)
		err = ioutil.WriteFile(rootDir+"/start.sh", []byte(startShellData), 0666)
		if err != nil {
			return err
		}
		stopShellData := projectReplacer.Replace(templateStopShell)
		err = ioutil.WriteFile(rootDir+"/stop.sh", []byte(stopShellData), 0666)
		if err != nil {
			return err
		}
	}

	return nil
}
